﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Knight.Common.Excel
{
    /// <summary>
    /// 操作EXCEL导出数据报表的类
    /// </summary>
    public class DataToExcel
    {
        public DataToExcel(DateTime beforeTime, DateTime afterTime)
        {
            this._beforeTime = beforeTime;
            this._afterTime = afterTime;
        }

        #region 操作EXCEL的一个类(需要Excel.dll支持)

        /// <summary>
        /// 标题背景色
        /// </summary>
        public int TitleColorIndex { set; get; } = 15;

        private readonly DateTime _beforeTime;			//Excel启动之前时间
        private readonly DateTime _afterTime;				//Excel启动之后时间

        #region 创建一个Excel示例
        /// <summary>
        /// 创建一个Excel示例
        /// </summary>
        public void CreateExcel()
        {
            //Excel.Application excel = new Excel.Application();
            //excel.Application.Workbooks.Add(true);
            //excel.Cells[1, 1] = "第1行第1列";
            //excel.Cells[1, 2] = "第1行第2列";
            //excel.Cells[2, 1] = "第2行第1列";
            //excel.Cells[2, 2] = "第2行第2列";
            //excel.Cells[3, 1] = "第3行第1列";
            //excel.Cells[3, 2] = "第3行第2列";

            ////保存
            //excel.ActiveWorkbook.SaveAs("./tt.xls", XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
            ////打开显示
            //excel.Visible = true;
            ////			excel.Quit();
            ////			excel=null;            
            ////			GC.Collect();//垃圾回收
        }
        #endregion

        #region 将DataTable的数据导出显示为报表

        /// <summary>
        /// 将DataTable的数据导出显示为报表
        /// </summary>
        /// <returns></returns>
        /// <summary>
        /// 结束Excel进程
        /// </summary>
        public void KillExcelProcess()
        {
            var myProcesses = Process.GetProcessesByName("Excel");

            //得不到Excel进程ID，暂时只能判断进程启动时间
            foreach (var myProcess in myProcesses)
            {
                var startTime = myProcess.StartTime;
                if (startTime > _beforeTime && startTime < _afterTime)
                {
                    myProcess.Kill();
                }
            }
        }
        #endregion

        #endregion

        #region 将DataTable的数据导出显示为报表(不使用Excel对象，使用COM.Excel)





        #endregion

        #region  清理过时的Excel文件
        private void ClearFile(string filePath)
        {
            var files = System.IO.Directory.GetFiles(filePath);
            if (files.Length > 10)
            {
                for (int i = 0; i < 10; i++)
                {
                    try
                    {
                        System.IO.File.Delete(files[i]);
                    }
                    catch
                    {
                        // ignored
                    }
                }
            }
        }
        #endregion

    }
}
